package cn.fl.yzt.service.impl;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.yzt.service.IYztTrackInfoService;
import cn.fl.yzt.transform.YztTrackInfoVOTransform;
import cn.fl.yzt.vo.YztTrackInfoVO;
import fl.yzt.facade.yzt.api.IYztTrackInfoApiService;
import fl.yzt.facade.yzt.condition.YztTrackInfoQueryCondition;
import fl.yzt.facade.yzt.constant.YztTrackInfoConstant;
import fl.yzt.facade.yzt.dto.YztTrackInfoDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 一证通线索表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class YztTrackInfoServiceImpl implements IYztTrackInfoService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(YztTrackInfoServiceImpl.class);
	
	/**
	 * 一证通线索表 暴露服务
	 */
	@Autowired
	private IYztTrackInfoApiService yztTrackInfoApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysRegionApiService sysRegionApiService;

	@Override
	public YztTrackInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		YztTrackInfoDTO dto = this.yztTrackInfoApiService.selectYztTrackInfoById(id);
        YztTrackInfoVO vo = YztTrackInfoVOTransform.toVO(dto);
        if(null != vo){
            this.setDictData(vo);
        }
        return vo;
	}

	@Override
	public Long add(YztTrackInfoVO entity) throws BaseException{
		if(null == entity) return null;
		return this.yztTrackInfoApiService.addYztTrackInfo(YztTrackInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.yztTrackInfoApiService.removeYztTrackInfoByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(YztTrackInfoVO entity) throws BaseException{
		if(null == entity) return false;
		return this.yztTrackInfoApiService.modifyYztTrackInfo(YztTrackInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        YztTrackInfoQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new YztTrackInfoQueryCondition());
        }
		condition=(YztTrackInfoQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.yztTrackInfoApiService.searchYztTrackInfoListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<YztTrackInfoVO> voList=YztTrackInfoVOTransform.toVOList((List<YztTrackInfoDTO>)page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<YztTrackInfoVO> searchList(YztTrackInfoQueryCondition condition) throws BaseException{
		if(null == condition) condition=new YztTrackInfoQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<YztTrackInfoVO> voList=YztTrackInfoVOTransform.toVOList(this.yztTrackInfoApiService.searchYztTrackInfoList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<YztTrackInfoVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
        List<String> prCdList = new ArrayList<>(voList.size());
		for(YztTrackInfoVO vo : voList){
		    if(!prCdList.contains(vo.getRgnPrCd())){
		        prCdList.add(vo.getRgnPrCd());
            }
        }
        Map<String,SysRegionDTO> rgnMap = this.sysRegionApiService.searchRegionByRgnCd(prCdList,CurrentThreadContext.getCurrentSysCd());
		Map<String, String>  budgetCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), YztTrackInfoConstant.CODE_BUDGET_CD);
		Map<String, String> stsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), YztTrackInfoConstant.CODE_STS_CD);
		for(YztTrackInfoVO vo:voList){
			if (budgetCdMap != null && budgetCdMap.containsKey(vo.getBudgetCd())) {
				vo.setBudgetCdNm(budgetCdMap.get(vo.getBudgetCd()));
			}
			if (stsCdMap != null && stsCdMap.containsKey(vo.getStsCd())) {
				vo.setStsCdNm(stsCdMap.get(vo.getStsCd()));
			}
			if(rgnMap != null && rgnMap.containsKey(vo.getRgnPrCd())){
			    vo.setRgnPrCdNm(rgnMap.get(vo.getRgnPrCd()).getRgnNm());
            }
		}
	}

    /**
     * 添加数据字典名称
     * @param vo
     */
    public void setDictData(YztTrackInfoVO vo){
        if(null == vo) return;
        Map<String, String>  budgetCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), YztTrackInfoConstant.CODE_BUDGET_CD);
        Map<String, String> stsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), YztTrackInfoConstant.CODE_STS_CD);
        if (budgetCdMap != null && budgetCdMap.containsKey(vo.getBudgetCd())) {
            vo.setBudgetCdNm(budgetCdMap.get(vo.getBudgetCd()));
        }
        if (stsCdMap != null && stsCdMap.containsKey(vo.getStsCd())) {
            vo.setStsCdNm(stsCdMap.get(vo.getStsCd()));
        }
        if(StringUtils.isNotBlank(vo.getRgnPrCd())){
            SysRegionDTO rgnDTO = this.sysRegionApiService.getRegionByRgnCd(vo.getRgnPrCd(),CurrentThreadContext.getCurrentSysCd());
            if(rgnDTO != null){
                vo.setRgnPrCdNm(rgnDTO.getRgnNm());
            }
        }

        if(null != vo.getCrtTm()){
            vo.setCrtTms(DateUtils.formatDateToString(vo.getCrtTm(), DateUtils.YYYY_MM_DDHHMM_ZH));
        }
        if(null != vo.getMdfTm()){
            vo.setMdfTms(DateUtils.formatDateToString(vo.getMdfTm(), DateUtils.YYYY_MM_DDHHMM_ZH));
        }
    }
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询省份
     * @return
     * @throws BaseException
     */
    @Autowired
    public List<KeyValueVO> searchRgnPrCdList() throws BaseException {
        List<KeyValueVO> result = null;
        List<SysRegionDTO> rgnList = this.sysRegionApiService.searchProvinceList(CurrentThreadContext.getCurrentSysCd());
        if(CollectionUtils.isNotEmpty(rgnList)){
            result = new ArrayList<>(rgnList.size());
            KeyValueVO kv = null;
            for(SysRegionDTO dto : rgnList){
                kv = new KeyValueVO();
                kv.setKey(dto.getRgnCd());
                kv.setValue(dto.getRgnNm());
                result.add(kv);
            }
        }
        return result;
    }
}
